package com.plugin.StatusBarNotification;


import com.ServiceBaby.app.R;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by GuoDapeng on 15/8/23.
 */
public class StatusBarNotification extends CordovaPlugin {
    //  Action to execute
    public static final String NOTIFY = "notify";
    public static final String CLEAR = "clear";

    /**
     * Executes the request and returns PluginResult
     *
     * @param action          Action to execute
     * @param args            JSONArray of arguments to the plugin
     * @param callbackContext The callback id used when calling back into JavaScript
     * @return A PluginRequest object with a status
     */
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        Log.d("NotificationPlugin", action);
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) this.cordova.getActivity().getSystemService(ns);
        context = this.cordova.getActivity().getApplicationContext();
        if (NOTIFY.equals(action)) {
            try {
                String title = args.getString(0);
                String body = args.getString(1);
                Log.d("NotificationPlugin", "Notification: " + title + ", " + body);
                showNotification(title, body);
                return true;
            } catch (JSONException jsonEx) {
                Log.d("NotificationPlugin", "Got JSON Exception " + jsonEx.getMessage());
                return false;
            }
        } else if (CLEAR.equals(action)) {
            clearNotification();
            return true;
        } else {
            Log.d("NotificationPlugin", "Invalid action : " + action + " passed");
        }
        return false;
    }

    /**
     * Displays status bar notification
     *
     * @param contentTitle Notification title
     * @param contentText  Notification text
     */
    public void showNotification(CharSequence contentTitle, CharSequence contentText) {
        int icon = R.drawable.icon;
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, contentTitle, when);
        //notification.flags |= Notification.FLAG_NO_CLEAR; //Notification cannot be clearned by user

        Intent notificationIntent = new Intent((Context) this.cordova.getActivity(), this.cordova.getActivity().getClass());
        PendingIntent contentIntent = PendingIntent.getActivity((Context) this.cordova.getActivity(), 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        mNotificationManager.notify(1, notification);
    }

    /**
     * Removes the Notification from status bar
     */
    public void clearNotification() {
        mNotificationManager.cancelAll();
        Log.d("NotificationPlugin", "cancelAll");
    }

    private NotificationManager mNotificationManager;
    private Context context;
}
